import { EventEmitter, Injectable } from '@angular/core'
import { routesApp } from '@/app/routes/routes-app'

export interface MenuTag {
  color: string // Background Color
  value: string
}

export interface MenuChildrenItem {
  route: string
  name: string
  type: 'link' | 'sub' | 'extLink' | 'extTabLink'
  children?: MenuChildrenItem[]
}

export interface Menu {
  route: string
  name: string
  type: 'link' | 'sub' | 'extLink' | 'extTabLink'
  icon: string
  label?: MenuTag
  badge?: MenuTag
  children?: MenuChildrenItem[]
}

@Injectable({
  providedIn: 'root'
})
export class MenuService {
  handleSubmenuClick = new EventEmitter<any>()
  handleSubmenuClicked = new EventEmitter<any>()

  routesApp = routesApp
  uniqueOpened: boolean
  activeIndex = ''
  openedMenus = []
  currentMenus = []
  routes = []

  constructor() {
    this.handleRoutes()
  }

  handleRoutes(): void {
    this.routesApp.map(r => {
      if (typeof r.children !== 'undefined') {
        if (r.children ?. length === 1) {
          r.path = `/${r.children[0].path}`
          r.children[0].path = `/${r.children[0].path}`
        } else {
          r.children.forEach(item => {
            item.path = `/${r.path}/${item.path}`
          })
        }
        this.routes.push(r)
      }
    })
  }

  getMenu(): object[] {
    return this.routes
  }

  setCurrentMenus(item): void {
    // debugger
    if (item) {
      this.currentMenus = item
    }
  }

  getCurrentMenus(): object[] {
    return this.currentMenus
  }
}
